<?php

/**
 * vote actions.
 *
 * @package    lomejorde
 * @subpackage vote
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class voteActions extends sfActions
{
    public function executeVote(sfWebRequest $request)
    {
        $user = $this->getUser()->getGuardUser();
        
        $this->form = new votingForm($user);
        
        $this->setLayout(false);
    }
    
    public function executeProccessVotes(sfWebRequest $request)
    {
        $user = $this->getUser()->getGuardUser();
        
        $this->form = new votingForm($user);

        $this->processForm($request, $this->form);

        $this->setTemplate('vote');
        
        $this->setLayout(false);
    }

    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid())
        {
            $form->save();

            $this->redirect($this->getController()->genUrl(
                    array(
                        'sf_route' => 'votaciones_show',
                        'sf_subject' => $this->getUser()->getGuardUser()
                    ), 
                    false
                ));
        }
    }
    
    public function executeShow(sfWebRequest $request)
    {
        $user =  $this->getRoute()->getObject();
        
        $this->votes = $user->getVotes();
        
        $this->pending_categories = Doctrine::getTable('Category')->getCategoriesNotIn($user->getCategoriesDoneVoting());
        
        $this->short_url = $this->get_bitly_short_url(
                $this->getController()->genUrl(array('sf_route' => 'show_user_nominations', 'sf_subject' => $user), true),
                sfConfig::get('app_bitly_login'),
                sfConfig::get('app_bitly_apikey')
        );
        
        $this->fb_app_id = sfConfig::get('app_facebook_appid');
        
        $this->setLayout(false);
    }
    
    /* returns the shortened url */
    protected function get_bitly_short_url($url, $login, $apikey, $format='txt') {
        $connectURL = 'http://api.bit.ly/v3/shorten?login='.$login.'&apiKey='.$apikey.'&longUrl='.urlencode($url).'&format='.$format;
        return $this->curl_get_result($connectURL);
    }

    /* returns expanded url */
    protected function get_bitly_long_url($url, $login, $apikey, $format='txt') {
        $connectURL = 'http://api.bit.ly/v3/expand?login='.$login.'&apiKey='.$apikey.'&shortUrl='.urlencode($url).'&format='.$format;
        return $this->curl_get_result($connectURL);
    }

    /* returns a result form url */
    protected function curl_get_result($url) {
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}
